home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / machserver / 1.098 / libc / strncmp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-23  |  1.7 KB  |  64 lines

  1. /* 
  2.  * strncmp.c --
  3.  *
  4.  *    Source code for the "strncmp" library routine.
  5.  *
  6.  * Copyright 1988 Regents of the University of California
  7.  * Permission to use, copy, modify, and distribute this
  8.  * software and its documentation for any purpose and without
  9.  * fee is hereby granted, provided that the above copyright
  10.  * notice appear in all copies.  The University of California
  11.  * makes no representations about the suitability of this
  12.  * software for any purpose.  It is provided "as is" without
  13.  * express or implied warranty.
  14.  */
  15.  
  16. #ifndef lint
  17. static char rcsid[] = "$Header: /sprite/src/lib/c/string/RCS/strncmp.c,v 1.2 89/03/22 16:07:11 rab Exp $ SPRITE (Berkeley)";
  18. #endif /* not lint */
  19.  
  20. #include <string.h>
  21.  
  22. /*
  23.  *----------------------------------------------------------------------
  24.  *
  25.  * strncmp --
  26.  *
  27.  *    Compares two strings lexicographically.
  28.  *
  29.  * Results:
  30.  *    The return value is 0 if the strings are identical in their
  31.  *    first s1 characters.  If they differ in their first s1
  32.  *    characters, then the return value is 1 if the first string is
  33.  *    greater than the second, and -1 if the second string is less
  34.  *    than the first.  If one string is a prefix of the other then
  35.  *    it is considered to be less (the terminating zero byte participates
  36.  *    in the comparison).
  37.  *
  38.  * Side effects:
  39.  *    None.
  40.  *
  41.  *----------------------------------------------------------------------
  42.  */
  43.  
  44. int
  45. strncmp(s1, s2, numChars)
  46.     register char *s1, *s2;        /* Strings to compare. */
  47.     register int numChars;        /* Max number of chars to compare. */
  48. {
  49.     for ( ; numChars > 0; numChars -= 1) {
  50.     if (*s1 != *s2) {
  51.         if (*s1 > *s2) {
  52.         return 1;
  53.         } else {
  54.         return -1;
  55.         }
  56.     }
  57.     if (*s1++ == 0) {
  58.         return 0;
  59.     }
  60.     s2 += 1;
  61.     }
  62.     return 0;
  63. }
  64.